package com.sunny.myproject.sales.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.sales.entity.SalesContract;
import com.sunny.myproject.sales.service.SalesContractService;

@Controller
@RequestMapping("/sales/salesContract")
public class SalesContractAction extends BaseAction<SalesContract>{

	private String title = "销售合同";
	private String action = "sales/salesContract";
	private String function = "salesContract";

	private Logger logger = Logger.getLogger(SalesContractAction.class);
	
	@Autowired
	private SalesContractService service;
	
	@Override
	@RequestMapping(params="add")
	public String add(HttpServletRequest arg0, HttpServletResponse arg1) {
		super.add(title, arg0);
		return "sales/salesContractEdit";
	}

	@Override
	@RequestMapping(params="delete")
	public String delete(HttpServletRequest arg0, HttpServletResponse arg1) {
		try {
			service.deleteByIds(super.param2(arg0));
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/sales/salesContract?list";
	}

	@Override
	@RequestMapping(params="edit")
	public String edit(Model arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		try {
			super.returnValue(arg0,
					service.findById(super.edit(title, arg0, arg1)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "sales/salesContractEdit";
	}

	@Override
	@RequestMapping(params="list")
	public String list(Model arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		try {
			super.pager = super.pageStart(arg0, arg1, arg2, action, function,
					title);
			super.pager = super.pageEnd(arg0, service.num(criterions), pager);
			super.returnValue(arg0, service.find(criterions, pager));
		} catch (Exception e) {
			logger.error(e);
		}
		return "sales/salesContractList";
	}

	@Override
	@RequestMapping(params="save")
	public String save(HttpServletRequest arg0, HttpServletResponse arg1,
			SalesContract arg2) {
		try {
			service.save(arg2);
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/sales/salesContract?list";
	}

	@Override
	@RequestMapping(params="view")
	public String view(Model arg0, HttpServletRequest arg1,
			HttpServletResponse arg2) {
		try {
			super.returnViewValue(arg0,
					service.findById(super.view(title, arg0, arg1)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "sales/salesContractEdit";
	}

}
